<template>
    <div class="song-list">
        <div class="song-item" v-for="(song,index) in songList" :key="index"
             :class="[index === curSelectSong?'song-select':'',song.id + '' === currentPlaySong.id + ''?'song-playing':'']"
             @click="selectSong(index)"
             @dblclick="selectItem(song,index)"
        >
            <div class="song-item-info-box">
                <div class="play-status-icon" v-if="song.mid === currentPlaySong.mid">
                    <Icon type="ios-play" size="17" v-if="playStatus"/>
                    <Icon type="ios-pause" size="17" v-else/>
                </div>
                <div class="song-info-box">
                    <div class="song-name">
                        <span class="title" v-html="song.title"></span>
                        <span class="subTitle" v-if="song.subTitle" v-html="'('+song.subTitle+')'"></span>
                    </div>
                    <div class="song-singer-and-time">
                        <div class="singer">
                             <span v-for="(singer) in song.singers" :key="singer.singerMid"
                                   v-html="singer.singerName" :title="singer.singerName"
                                   @click="toSingerDetail(singer)" @dblclick.stop=""></span>
                        </div>
                        <div class="song-time">
                            {{_handleTime(song.duration)}}
                        </div>
                    </div>
                </div>
            </div>
            <div class="song-item-tools-box">
                <!--收藏按钮-->
                <i class="btn love-btn fa fa-heart-o" aria-hidden="true" title="收藏"
                   @click="saveFavoriteSong(song)"
                   v-if="isFavoriteSong(song)"></i>
                <i class="btn unlove-btn fa fa-heart" aria-hidden="true" title="取消收藏"
                   @click="deleteFavoriteSong(song)"
                   v-else></i>
                <Icon class="btn clear-icon" type="md-trash" size="20" @click="deleteSong(song)" @dblclick.stop=""/>
            </div>
        </div>
    </div>
</template>

<script>

  import { Icon } from 'view-design'
  import { handleTime } from '../../../../common/js/util'
  import { mapGetters } from 'vuex'

  export default {
    name: 'm-play-list-song-list',
    components: {
      Icon
    },
    props: {
      currentPlaySong: {
        type: Object
      },
      songList: {
        type: Array
      },
      playStatus: {
        type: Boolean
      },
      curSelectSong: {
        type: Number
      }
    },
    computed: {
      ...mapGetters([
        'loginUser',
        'favoriteSongList'
      ]),
      isFavoriteSong () {
        return (song) => {
          // 未登录
          if (this.loginUser === null || this.favoriteSongList === null) {
            return true
          }
          return this.favoriteSongList.findIndex((item) => item.id + '' === song.id + '') === -1
        }
      }
    },
    methods: {
      selectSong (index) {
        this.$emit('selectSong', index)
      },
      selectItem (song, index) {
        this.$emit('selectItem', song, index)
      },
      toSingerDetail (singer) {
        this.$emit('toSingerDetail', singer)
      },
      // 处理歌曲播放时长
      _handleTime (duration) {
        return handleTime(duration)
      },
      saveFavoriteSong (song) {
        this.$emit('saveFavoriteSong', song)
      },
      deleteFavoriteSong (song) {
        this.$emit('deleteFavoriteSong', song)
      },
      deleteSong (song) {
        this.$emit('deleteSong', song)
      }
    }
  }
</script>

<style lang="less">
    .song-list {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: flex-start;
        flex-flow: column;

        .song-item {
            width: 100%;
            position: relative;
            height: 80px;
            background: var(--play-list-background-color);
            display: flex;
            justify-content: center;
            align-items: center;

            .song-item-info-box {
                position: relative;
                width: 100%;
                height: 100%;
                padding-left: 30px;

                .play-status-icon {
                    position: absolute;
                    left: 0;
                    width: 30px;
                    height: 100%;
                    color: var(--font-active-color);
                    display: flex;
                    justify-content: center;
                    align-items: center;
                }

                .song-info-box {
                    width: 100%;
                    height: 100%;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    flex-flow: column;

                    .song-name {
                        width: 100%;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        font-size: 14px;
                        color: var(--font-base-color);

                        .subTitle {
                            color: var(--font-tow-color) !important;
                        }
                    }

                    .song-singer-and-time {
                        font-size: 12px;
                        width: 100%;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;

                        .singer {
                            overflow: hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                            color: var(--font-tow-color);

                            span {
                                margin-right: 5px;
                                cursor: pointer;
                            }

                            span:hover {
                                color: var(--font-active-color);
                            }
                        }

                        .song-time {
                            width: 40px;
                            color: var(--font-tow-color);
                            display: flex;
                            justify-content: center;
                            align-items: center;
                        }
                    }
                }
            }

            .song-item-tools-box {
                height: 100%;
                width: 0;
                display: flex;
                justify-content: flex-end;
                align-items: center;
                overflow: hidden;

                .btn {
                    height: 100%;
                    width: 30px;
                    font-size: 16px;
                    color: var(--font-tow-color);
                    display: flex;
                    justify-content: center;
                    align-items: center;
                }

                .btn:hover {
                    cursor: pointer;
                    color: var(--font-active-color);
                }

                .love-btn {
                    color: var(--no-favorite-color);
                }

                .unlove-btn {
                    color: var(--favorite-color);
                }

                .unlove-btn:hover {
                    color: var(--favorite-color);
                }

                .love-btn:hover {
                    color: var(--favorite-color);
                }
            }
        }

        .song-item:hover {
            background: var(--song-list-hover-background-color);

            .song-item-info-box {
                width: 80%;

                .song-info-box {

                    .song-singer-and-time {

                        .singer {
                            overflow: hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                            color: var(--font-base-color);
                        }
                    }

                    .song-time {
                        opacity: 0;
                    }
                }
            }

            .song-item-tools-box {
                width: 20%;
            }
        }

        .song-select {
            background: var(--song-list-hover-background-color) !important;

            .song-item-info-box {
                width: 80%;

                .song-info-box {

                    .song-singer-and-time {

                        .singer {
                            overflow: hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                            color: var(--font-base-color);
                        }
                    }

                    .song-time {
                        opacity: 0;
                    }
                }
            }

            .song-item-tools-box {
                width: 20%;
            }
        }

        .song-playing {
            background: var(--song-list-hover-background-color) !important;

            .song-item-info-box {
                width: 80%;

                .song-info-box {

                    .song-name {
                        color: var(--font-active-color);

                        .subTitle {
                            color: var(--font-active-color) !important;
                        }
                    }

                    .song-singer-and-time {

                        .singer {
                            overflow: hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                            color: var(--font-active-color) !important;
                        }

                        .song-time {
                            opacity: 0;
                            color: var(--font-active-color) !important;
                        }
                    }
                }
            }

            .song-item-tools-box {
                width: 20%;
            }
        }
    }
</style>
